package com.tjs.lucky.infrastructure.repository;


import com.tjs.lucky.domain.strategy.model.aggregates.StrategyRich;
import com.tjs.lucky.domain.strategy.model.vo.AwardBriefVO;
import com.tjs.lucky.domain.strategy.model.vo.AwardRateVO;
import com.tjs.lucky.domain.strategy.model.vo.StrategyDetailBriefVO;
import com.tjs.lucky.domain.strategy.repository.IStrategyRepository;
import com.tjs.lucky.infrastructure.mapper.IAwardMapper;
import com.tjs.lucky.infrastructure.mapper.IStrategyDetailMapper;
import com.tjs.lucky.infrastructure.mapper.IStrategyMapper;
import com.tjs.lucky.infrastructure.po.Award;
import com.tjs.lucky.infrastructure.po.Strategy;
import com.tjs.lucky.infrastructure.po.StrategyDetail;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 田佳澍
 * @Description: 策略表仓储服务
 * @DateTime: 2023/5/16 10:44
 **/
@Repository
public class StrategyRepository implements IStrategyRepository {

    @Resource
    private IStrategyMapper strategyMapper;

    @Resource
    private IStrategyDetailMapper strategyDetailMapper;

    @Resource
    private IAwardMapper awardMapper;

    /**
     * 根据 **策略ID**查询 策略的完整信息**【策略配置 （strategy）＋ 策略明细（strategy_detail）==>聚合成一个类(StrategyRich) 】**
     *
     * @param strategyId 策略ID
     * @return 策略配置 和 策略明细 的聚合类
     */
    @Override
    public StrategyRich queryStrategyRich(Long strategyId) {
        Strategy strategy = strategyMapper.queryStrategyById(strategyId);
        List<StrategyDetail> strategyDetailList = strategyDetailMapper.queryStrategyDetailListById(strategyId);
        return new StrategyRich(
                strategyId,
                strategy.convertToBriefVO(),
                strategyDetailList.stream().map(StrategyDetail::convertToBriefVO).collect(Collectors.toList())
        );
    }

    @Override
    public List<AwardRateVO> queryStrategyDetailList(Long strategyId){
        List<StrategyDetail> strategyDetailList = strategyDetailMapper.queryStrategyDetailListById(strategyId);
        List<AwardRateVO> awardRateInfoList = new ArrayList<>(strategyDetailList.size());
        for (StrategyDetail strategyDetail : strategyDetailList) {
            awardRateInfoList.add(new AwardRateVO(String.valueOf(strategyDetail.getAwardId()), strategyDetail.getAwardRate()));
        }
        return awardRateInfoList;
    }

    /**
     * 根据 **奖品ID** 查询 奖品信息。
     *
     * @param awardId 奖品编号
     * @return 奖品详细信息
     */
    @Override
    public AwardBriefVO queryAwardInfo(String awardId) {
        Award award = awardMapper.queryAwardInfoById(awardId);
        return award.convertToBriefVO();
    }

    /**
     * 根据 策略ID 查询没有库存的奖品信息
     *
     * @param strategyId 策略ID
     * @return 没有库存的奖品信息
     */
    @Override
    public List<String> queryNoStockStrategyAwardList(Long strategyId) {
        return strategyDetailMapper.queryNoStockStrategyAwardList(strategyId);
    }

    @Override
    public void synAwardStock(Long strategyId, String awardId, String awardStock){
        StrategyDetail strategyDetail = new StrategyDetail();
        strategyDetail.setStrategyId(strategyId);
        strategyDetail.setAwardId(awardId);
        strategyDetail.setAwardCount(Integer.valueOf(awardStock));
        strategyDetailMapper.update(strategyDetail);
    }

    @Override
    public Integer queryTotalAwardStock(Long strategyId) {
        List<StrategyDetail> strategyDetailList = strategyDetailMapper.queryByStrategyId(strategyId);
        int totalAwardStock = 0;
        for (StrategyDetail strategyDetail : strategyDetailList) {
            Integer awardCount = strategyDetail.getAwardCount();
            if (awardCount == null || awardCount < 0) {
                totalAwardStock = -1;
                break;
            }
            totalAwardStock += awardCount;
        }
        return totalAwardStock;
    }

    /**
     * 该策略是否存在
     *
     * @param strategyId
     * @return
     */
    @Override
    public Boolean hasThisStrategy(Long strategyId) {
        Strategy strategy = strategyMapper.queryStrategyById(strategyId);
        return strategy != null;
    }
}
